package com.project.controller;

import com.project.bean.ShopBean;
import com.project.bean.UserBean;
import com.project.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("custom/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @RequestMapping("buy")
    public String buy (int productId, HttpServletRequest request){
        //从会话中 得到登录信息
        UserBean loginUser= (UserBean) request.getSession().getAttribute("loginUser");
        //调用业务方法，完成购物车添加
        shopService.add(productId,loginUser.getId());

        return "ok";

    }

    /**
     * 查询登录用户的购物信息
     * @param request 请求对象
     * @return 购物车集合
     */
    @RequestMapping("findByUser")
    public  List<ShopBean>findByUser(HttpServletRequest request){
        //从session中获得登录用户
        UserBean loginUser= (UserBean) request.getSession().getAttribute("loginUser");

        return shopService.findAll(loginUser.getId());

    }

    @RequestMapping("del")
    public  String del(int shopId){
        shopService.del(shopId);
        return "ok";
    }

    /**
     * 修改购物车商品数量
     * @param shopId 购物车ID
     * @param newNum 新数量
     * @return 是否成功
     */
    @RequestMapping("updateNum")
    public  String update(int shopId,int newNum){
        shopService.updateNum(shopId,newNum);

        return  "ok";
    }

}
